package com.heima.behavior.service.impl;
import cn.hutool.json.JSONUtil;
import com.heima.model.article.mess.UpdateArticleMess.UpdateArticleType;

import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.model.article.mess.UpdateArticleMess;
import com.heima.model.behavior.dto.LikesBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author TheCai
 * @date 2021/6/4 21:54
 */
@Service
@Slf4j
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior>
    implements ApLikesBehaviorService {
  @Autowired ApBehaviorEntryService apBehaviorEntryService;

  @Override
  public ResponseResult like(LikesBehaviorDto dto) {
    // 1.检验参数
    if (dto.getArticleId() == null || dto.getOperation() == null || dto.getEquipmentId() == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    ApUser user = AppThreadLocalUtils.getUser();
    if (user == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
    }
    // 找到实体
    ApBehaviorEntry apBehaviorEntry =
        apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());

    if (apBehaviorEntry == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 找到点赞行为,看看有没有
    if (dto.getOperation().intValue() == 0) {
      // 点赞
      ApLikesBehavior apLikesBehavior =
          this.getOne(
              Wrappers.<ApLikesBehavior>lambdaQuery()
                  .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                  .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));
      // 保存点赞
      if (apLikesBehavior != null) {
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "请勿重复点赞");
      }
      apLikesBehavior = new ApLikesBehavior();
      apLikesBehavior.setEntryId(apBehaviorEntry.getId());
      apLikesBehavior.setArticleId(dto.getArticleId());
      apLikesBehavior.setType(dto.getType());
      apLikesBehavior.setOperation(dto.getOperation());
      apLikesBehavior.setCreatedTime(new Date());
      this.save(apLikesBehavior);

    } else {
      // 取消点赞
      this.remove(
          Wrappers.<ApLikesBehavior>lambdaQuery()
              .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
              .eq(ApLikesBehavior::getArticleId, dto.getArticleId()));
    }
    //计算热点文章消息封装
    UpdateArticleMess updateArticleMess=new UpdateArticleMess();
    updateArticleMess.setType(UpdateArticleType.LIKES);//当前是点赞行为
    updateArticleMess.setArticleId(dto.getArticleId());
    updateArticleMess.setAdd(dto.getOperation().intValue()==0?1:-1);//如果点赞文章分值加一,取消点赞文章分值减一

    kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSONUtil.toJsonStr(updateArticleMess));
    log.info("点赞行为 热点文章计算 消息已发送  消息内容===>> {}",updateArticleMess);
    return ResponseResult.okResult();
  }
  @Autowired
  KafkaTemplate<String,String> kafkaTemplate;
}
